package uni.androidgame.mapgeneration;

import android.graphics.Canvas;

import java.util.ArrayList;

import uni.androidgame.gameobjects.Tile;

public class Room
{
    private Tile[][] _tiles;
    private int _x;
    public int getX(){return _x;}
    private int _y;
    public int getY(){return _y;}
    private int _width;
    public int getWidth(){return _width;}
    private int _height;
    public int getHeight(){return _height;}
    public int getRightSide(){return _x+_width-1;}
    public int getBottomSide(){return _y+_height-1;}


    public boolean isLeftOf(Room otherRoom)
    {
        return getRightSide()<otherRoom.getX();
    }

    public boolean isAboveOf(Room otherRoom)
    {
        return getBottomSide()<otherRoom.getY();
    }

    public Room(int x, int y, int width, int height)
    {
        _x = x;
        _y = y;
        _width = width;
        _height = height;
        _tiles = new Tile[_width][_height];
    }


    public void setWallsAndFloor()
    {
        for(int x = 0;x<_width;x++)
        {
            for(int y=0;y<_height;y++)
            {
                if(x==0||y==0||x==_width-1||y==_height-1)
                {
                    _tiles[x][y] = new Tile(Tile.Types.Wall,x+_x,y+_y,this);
                }
                else
                {
                    _tiles[x][y] = new Tile(Tile.Types.Floor,x+_x,y+_y,this);
                }
            }
        }
        for(int x=0;x<getWidth();x++)
        {
            for(int y= 0;y<getHeight();y++)
            {
                setNeighbours(_tiles[x][y]);
            }
        }
    }

    public void setNeighbours(Tile tile)
    {
        int x = tile.getX()-_x;
        int y = tile.getY()-_y;

        //add 8 directions instead of 4
        ArrayList<Tile> neighbourList = new ArrayList<Tile>();
        if(x-1>=0 && _tiles[x-1][y].getType()==Tile.Types.Floor)
        {
            neighbourList.add(_tiles[x-1][y]);
        }
        if(y-1>=0 && _tiles[x][y-1].getType()==Tile.Types.Floor)
        {
            neighbourList.add(_tiles[x][y-1]);
        }
        if(x+1<_width && _tiles[x+1][y].getType()==Tile.Types.Floor)
        {
            neighbourList.add(_tiles[x+1][y]);
        }
        if(y+1<_height && _tiles[x][y+1].getType()==Tile.Types.Floor)
        {
            neighbourList.add(_tiles[x][y+1]);
        }

        tile.setNeighbours(neighbourList);
    }
    public void placeDoor(int x, int y,Tile corridorEntrance)
    {
        _tiles[x-_x][y-_y].setType(Tile.Types.Door);
        setNeighbours(_tiles[x - _x][y - _y]);
        _tiles[x-_x][y-_y].addNeighbour(corridorEntrance);
        corridorEntrance.addNeighbour(_tiles[x-_x][y-_y]);
        if(x-_x==_width-1)
        {
            _tiles[x-_x-1][y-_y].addNeighbour(_tiles[x-_x][y-_y]);
        }
        else if(x-_x==0)
        {
            _tiles[x-_x+1][y-_y].addNeighbour(_tiles[x-_x][y-_y]);
        }
        else if(y-_y==_height-1)
        {
            _tiles[x-_x][y-_y-1].addNeighbour(_tiles[x-_x][y-_y]);
        }
        else if(y-_y==0)
        {
            _tiles[x-_x][y-_y+1].addNeighbour(_tiles[x-_x][y-_y]);
        }
    }

    public Tile getTile(int worldX, int worldY)
    {
        int localX = worldX-_x;
        int localY = worldY-_y;
        if(localX>=0 && localY >= 0 && localX<_tiles.length && localY<_tiles[0].length)
        {
            return _tiles[localX][localY];
        }
        else
        {
            return null;
        }
    }
    public void draw(Canvas canvas)
    {
        for(int x = 0;x<_width;x++)
        {
            for(int y= 0;y<_height;y++)
            {
                _tiles[x][y].draw(canvas);
            }
        }
    }
}
